package cn.wolfcode.shop.cloud.web.controller;


import cn.wolfcode.shop.cloud.Result;
import cn.wolfcode.shop.cloud.member.domain.User;
import cn.wolfcode.shop.cloud.msg.SeckillGoodsCodeMsg;
import cn.wolfcode.shop.cloud.service.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

@Controller
public class PathController {
    @Autowired
    private RedisService redisService;

    @RequestMapping("/getPath")
    @ResponseBody
    public Result getPath(String uuid, String verifyCode){
        //校验验证码是否为空和匹配
        if(StringUtils.isEmpty(verifyCode)){
            return Result.error(SeckillGoodsCodeMsg.VERIFY_CODE_EMPTY);
        }
        Integer redisVerifyCode =redisService.get(SeckillKeyPrefix.SECKILL_VERIFYCODE,uuid,Integer.class);
        if(!verifyCode.equals(redisVerifyCode+"")){
            return Result.error(SeckillGoodsCodeMsg.SECKILL_VERIFY_CODE_ERROR);
        }
        String path = UUID.randomUUID().toString().replaceAll("_","");
        redisService.set(SeckillKeyPrefix.SECKILL_PATH,uuid,path);
        return Result.success(path);
    }
}
